﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KerriganDataContract;
using System.Web.Security;
using System.Diagnostics;
using KerriganDataContract.Forum;

namespace KerriganForum
{
    public partial class Home : BasePage
    {

        private string requestCookie;
        private bool isAnonymous;
        private int iCount = 0;


        private int ID
        {
            get
            {
                if (ViewState["id"] == null)
                    ViewState["id"] = -1;
                return (int)ViewState["id"];
            }
            set
            {
                ViewState["id"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                using (ServiceProxy Srv = new ServiceProxy(Request.Cookies))
                {

                    try
                    {

                        requestCookie = Request.Cookies["BXPXAUTHFWCF"].Value;

                    }
                    catch (Exception ex)
                    {
                        requestCookie = null;
                    }

                    if (Identify.Anonymous == null || requestCookie == null || Identify.IdUser == null)
                    {
                        Identify.CheckUser(requestCookie);
                    }
                    else if (requestCookie != null && Identify.Anonymous == true)
                    {
                        Identify.CheckUser(requestCookie);
                    }

                }
                CheckWebUI();
                buttonNoUse.Click += new EventHandler(this.btnSearch_Click);

                btn_add_Click(sender, e);

                using (ServiceProxy Srv = new ServiceProxy())
                {
                    GridView1.DataSource = Srv.Service.GetCategoryToDisplay();
                    GridView1.DataBind();
                }
            }
        }

        protected void GridView1_Paging(object sender, GridViewPageEventArgs e)
        {

            GridView1.PageIndex = e.NewPageIndex;
            RefreshDataGrid();
        }

        protected void CheckWebUI() {

            if (Identify.TypeUser == 1)
            {
                btnAddCategory.Visible = true;

            }
            else {

                btnAddCategory.Visible = false;
            }
        
        }


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
            using (ServiceProxy Srv = new ServiceProxy())
            {
                if (Identify.Anonymous != true)
                {

                    //List<vwGetForumTopicByPerson> bo = Srv.Service.GetTopicByPerson(ID, Identify.IdUser);

                    //vwGetForumTopicByPerson boFull = bo[iCount];

                    vwGetForumCategory category = e.Row.DataItem as vwGetForumCategory;
                    //(e.Row.FindControl("lnkEdit") as LinkButton).Attributes.Add("onClick", "ShowEditModal('" + category.IdForumCategory + "');");
                    //(e.Row.FindControl("lnkDelete") as LinkButton).CommandArgument = category.IdForumCategory.ToString();


                    if (Identify.TypeUser == 1)
                    {

                        (e.Row.FindControl("lnkEdit") as LinkButton).Visible = true;
                        (e.Row.FindControl("lnkDelete") as LinkButton).Visible = true;
                        (e.Row.FindControl("lnkEdit") as LinkButton).Attributes.Add("onClick", "ShowEditModal('" + category.IdForumCategory + "');");
                        (e.Row.FindControl("lnkDelete") as LinkButton).CommandArgument = category.IdForumCategory.ToString();

                    }
                    else
                    {
                        (e.Row.FindControl("lnkEdit") as LinkButton).Visible = false;
                        (e.Row.FindControl("lnkDelete") as LinkButton).Visible = false;

                    }

                }
                else
                {

                    (e.Row.FindControl("lnkEdit") as LinkButton).Visible = false;
                    (e.Row.FindControl("lnkDelete") as LinkButton).Visible = false;
                }

            }
            iCount++;

            }
        }


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyDelete")
            {
                using (ServiceProxy Srv = new ServiceProxy())
                {
                    int id = Convert.ToInt32(e.CommandArgument);
                    Srv.Service.DeleteCategory(id);
                    RefreshDataGrid();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            RefreshDataGrid();
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {

            buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
            btnAddCategory.Attributes.Add("onClick", "ShowNewModal();");
        }


        protected void RefreshDataGrid()
        {
            this.GridView1.DataSource = null;
            using (ServiceProxy Srv = new ServiceProxy())
            {

                GridView1.DataSource = Srv.Service.GetCategoryToDisplay();
                GridView1.DataBind();

                buttonNoUse.Click += new EventHandler(this.btnSearch_Click);

            }

        }


        protected void btnOkay_Click(object sender, EventArgs e)
        {

            AddCategory();
            ClientScript.RegisterStartupScript(this.GetType(), "onload", "onSuccess();", true);

        }



        private void AddCategory()
        {
            using (ServiceProxy Srv = new ServiceProxy())
            {

                ForumCategoryDC bo = new ForumCategoryDC();
                bo.NameCategory = txtNameCategory.Text;
                bo.Description = TxtDescCategory.InnerText;
                Srv.Service.AddCategory(bo);

            }

        }

    }
}